/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmediaplayer.threads;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jmediaplayer.FormMain;
import jmediaplayer.UsefulFunctions;
import jmediaplayer.setting;
import jtmdb.Movie;
import jtmdb.MovieImages;
import jtmdb.MoviePoster;
import org.json.JSONException;

/**
 *
 * @author dean.williams
 */
public class ThreadLoadMediaImage implements Runnable {

	private String id;
	private String hash;

	public ThreadLoadMediaImage(String vid, String vhash) {
		id = vid;
		hash = vhash;
	}

	public void GetImages(String id, String hash) throws IOException, JSONException {
		MovieImages images = Movie.getImages(Integer.parseInt(id));

		for (MoviePoster poster : images.posters) {
			System.out.println(poster.getSmallestImage());
			String file = UsefulFunctions.downloadImage(poster.getLargestImage().toString(), setting.get("tempDirectory") + id);

			//Get extension
			//int dot = file.lastIndexOf(".");
			//int slash = file.lastIndexOf("\\");
			//String name = file.substring(slash, dot);

			UsefulFunctions.resizeImage(file, setting.get("iconCacheDirectory") + hash + "_small.png", 80, 80);
			UsefulFunctions.resizeImage(file, setting.get("iconCacheDirectory") + hash + "_med.png", 200, 200);
			UsefulFunctions.resizeImage(file, setting.get("iconCacheDirectory") + hash + "_lrge.png", 300, 300);

			images = null;
			FormMain.MSOKActionPerformedComplete();
			break;
		}
	}

	@Override
	public void run() {
		try {
			GetImages(id, hash);
		} catch (IOException ex) {
			Logger.getLogger(ThreadLoadMediaImage.class.getName()).log(Level.SEVERE, null, ex);
		} catch (JSONException ex) {
			Logger.getLogger(ThreadLoadMediaImage.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
